﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pdfer.Encodings
{
    internal static class InternalEncoding
    {
        private readonly static Char[] STD;
        private readonly static Char[] MAC;
        private readonly static Char[] WIN;
        private readonly static Char[] PDF;

        private static readonly Dictionary<char, (int std, int mac, int win, int pdf)> charSet = new()
        {
            {'A', (0x41,0x41,0x41,0x41)},
            {'Æ', (0xe1,0xae,0xc6,0xc6)},
            {'Á', (0x00,0xe7,0xc1,0xc1)},
            {'Â', (0X00,0xe5,0xc2,0xc2)},
            {'Ä', (0X00,0x80,0xc4,0xc4)},
            {'À', (0x00,0xcb,0xc0,0xc0)},
            {'Å', (0x00,0x81,0xc5,0xc5)},
            {'Ã', (0x00,0xcc,0xc3,0xc3)},
            {'B', (0x42,0x42,0x42,0x42)},
            {'C', (0x43,0x43,0x43,0x43)},
            {'Ç', (0x00,0x82,0xc7,0xc7)},
            {'D', (0x44,0x44,0x44,0x44)},
            {'E', (0x45,0x45,0x45,0x45)},
            {'É', (0x00,0x83,0xc9,0xc9)},
            {'Ê', (0x00,0xe6,0xca,0xca)},
            {'Ë', (0x00,0xe8,0xcb,0xcb)},
            {'È', (0x00,0xe9,0xc8,0xc8)},
            {'Ð', (0x00,0x00,0xd0,0xd0)},
            {'€', (0x00,0x00,0x80,0xa0)},
            {'F', (0x46,0x46,0x46,0x46)},
            {'G', (0x47,0x47,0x47,0x47)},
            {'H', (0x48,0x48,0x48,0x48)},
            {'I', (0x49,0x49,0x49,0x49)},
            {'Í', (0x00,0xea,0xcd,0xcd)},
            {'Î', (0x00,0xeb,0xce,0xce)},
            {'Ï', (0x00,0xec,0xcf,0xcf)},
            {'Ì', (0x00,0xed,0xcc,0xcc)},
            {'J', (0x4a,0x4a,0x4a,0x4a)},
            {'K', (0x4b,0x4b,0x4b,0x4b)},
            {'L', (0x4c,0x4c,0x4c,0x4c)},
            {'Ł', (0xe8,0x00,0x00,0x95)},
            {'M', (0x4d,0x4d,0x4d,0x4d)},
            {'N', (0x4e,0x4e,0x4e,0x4e)},
            {'Ñ', (0x00,0x84,0xd1,0xd1)},
            {'O', (0x4f,0x4f,0x4f,0x4f)},
            {'Œ', (0xea,0xce,0x8c,0x96)},
            {'Ó', (0x00,0xee,0xd3,0xd3)},
            {'Ô', (0x00,0xef,0xd4,0xd4)},
            {'Ö', (0x00,0x85,0xd6,0xd6)},
            {'Ò', (0x00,0xf1,0xd2,0xd2)},
            {'Ø', (0xe9,0xaf,0xd8,0xd8)},
            {'Õ', (0x00,0xcd,0xd5,0xd5)},
            {'P', (0x50,0x50,0x50,0x50)},
            {'Q', (0x51,0x51,0x51,0x51)},
            {'R', (0x52,0x52,0x52,0x52)},
            {'S', (0x53,0x53,0x53,0x53)},
            {'Š', (0x00,0x00,0x8a,0x97)},
            {'T', (0x54,0x54,0x54,0x54)},
            {'Þ', (0x00,0x00,0xde,0xde)},
            {'U', (0x55,0x55,0x55,0x55)},
            {'Ú', (0x00,0xf2,0xda,0xda)},
            {'Û', (0x00,0xf3,0xda,0xdb)},
            {'Ü', (0x00,0x86,0xdc,0xdc)},
            {'Ù', (0x00,0xf4,0xd9,0xd9)},
            {'V', (0x56,0x56,0x56,0x56)},
            {'W', (0x57,0x57,0x57,0x57)},
            {'X', (0x58,0x58,0x58,0x58)},
            {'Y', (0x59,0x59,0x59,0x59)},
            {'Ý', (0x00,0x00,0xdd,0xdd)},
            {'Ÿ', (0x00,0xd9,0x9f,0x9f)},
            {'Z', (0x5a,0x5a,0x5a,0x5a)},
            {'Ž', (0x00,0x00,0x8e,0x99)},
            {'a', (0x61,0x61,0x61,0x61)},
            {'á', (0x00,0x87,0xe1,0xe1)},
            {'â', (0x00,0x89,0xe2,0xe2)},
            {'´', (0xc2,0xab,0xb4,0xb4)},
            {'ä', (0x00,0x8a,0xe4,0xe4)},
            {'æ', (0xf1,0xbe,0xe6,0xe6)},
            {'à', (0x00,0x88,0xe0,0xe0)},
            {'&', (0x26,0x26,0x26,0x26)},
            {'å', (0x00,0x8c,0xe5,0xe5)},
            {'^', (0x5e,0x5e,0x5e,0x5e)},
            {'~', (0x7e,0x7e,0x7e,0x7e)},
            {'*', (0x2a,0x2a,0x2a,0x2a)},
            {'@', (0x40,0x40,0x40,0x40)},
            {'ã', (0x00,0x8b,0xe3,0xe3)},
            {'b', (0x62,0x62,0x62,0x62)},
            {'\\', (0x5c,0x5c,0x5c,0x5c)},
            {'|', (0x7c,0x7c,0x7c,0x7c)},
            {'{', (0x7b,0x7b,0x7b,0x7b)},
            {'}', (0x7d,0x7d,0x7d,0x7d)},
            {'[', (0x5b,0x5b,0x5b,0x5b)},
            {']', (0x5d,0x5d,0x5d,0x5d)},
            {'˘', (0xc6,0xf9,0x00,0x18)},
            {'¦', (0x00,0x00,0xa6,0xa6)},
            {'•', (0xb7,0xa5,0x95,0x80)},
            {'c', (0x63,0x63,0x63,0x63)},
            {'ˇ', (0xcf,0xff,0x00,0x19)},
            {'ç', (0x00,0x8d,0xe7,0xe7)},
            {'¸', (0xcb,0xfc,0xb8,0xb8)},
            {'¢', (0xa2,0xa2,0xa2,0xa2)},
            {'ˆ', (0xc3,0xf6,0x88,0x1a)},
            {':', (0x3a,0x3a,0x3a,0x3a)},
            {',', (0x2c,0x2c,0x2c,0x2c)},
            {'©', (0x00,0xa9,0xa9,0xa9)},
            {'¤', (0xa8,0xdb,0xa4,0xa4)},
            {'d', (0x64,0x64,0x64,0x64)},
            {'†', (0xb2,0xa0,0x86,0x81)},
            {'‡', (0xb3,0xe0,0x87,0x82)},
            {'°', (0x00,0xa1,0xb0,0xb0)},
            {'¨', (0xc8,0xac,0xa8,0xa8)},
            {'÷', (0x00,0xd6,0xf7,0xf7)},
            {'$', (0x24,0x24,0x24,0x24)},
            {'˙', (0xc7,0xfa,0x00,0x1b)},
            {'ı', (0xf5,0xf5,0x00,0x9a)},
            {'e', (0x65,0x65,0x65,0x65)},
            {'é', (0x00,0x8e,0xe9,0xe9)},
            {'ê', (0x00,0x90,0xea,0xea)},
            {'ë', (0x00,0x91,0xeb,0xeb)},
            {'è', (0x00,0x8f,0xe8,0xe8)},
            {'8', (0x38,0x38,0x38,0x38)},
            {'…', (0xbc,0xc9,0x85,0x83)},
            {'—', (0xd0,0xd1,0x97,0x84)},
            {'–', (0xb1,0xd0,0x96,0x85)},
            {'=', (0x3d,0x3d,0x3d,0x3d)},
            {'ð', (0x00,0x00,0xf0,0xf0)},
            {'!', (0x21,0x21,0x21,0x21)},
            {'¡', (0xa1,0xc1,0xa1,0xa1)},
            {'f', (0x66,0x66,0x66,0x66)},
            // {'f', (0xae,0xde,0x00,0x93)},//fi 符号 编辑器显示不出来暂时忽略
            {'5', (0x35,0x35,0x35,0x35)},
            {'ƒ', (0xa6,0xc4,0x83,0x86)},
            {'4', (0x34,0x34,0x34,0x34)},
            {'⁄', (0xa4,0xda,0x00,0x87)},
            {'g', (0x67,0x67,0x67,0x67)},
            {'ß', (0xfb,0xa7,0xdf,0xdf)},
            {'`', (0xc1,0x60,0x60,0x60)},
            {'>', (0x3e,0x3e,0x3e,0x3e)},
            {'«', (0xab,0xc7,0xab,0xab)},
            {'»', (0xbb,0xc8,0xbb,0xbb)},
            {'‹', (0xac,0xdc,0x8b,0x88)},
            {'›', (0xad,0xdd,0x9b,0x89)},
            {'h', (0x68,0x68,0x68,0x68)},
            {'˝', (0xcd,0xfd,0x00,0x1c)},
            {'-', (0x2d,0x2d,0x2d,0x2d)},
            {'i', (0x69,0x69,0x69,0x69)},
            {'í', (0x00,0x92,0xed,0xed)},
            {'î', (0x00,0x94,0xee,0xee)},
            {'ï', (0x00,0x95,0xef,0xef)},
            {'ì', (0x00,0x93,0xec,0xec)},
            {'j', (0x6a,0x6a,0x6a,0x6a)},
            {'k', (0x6b,0x6b,0x6b,0x6b)},
            {'l', (0x6c,0x6c,0x6c,0x6c)},
            {'<', (0x3c,0x3c,0x3c,0x3c)},
            {'¬', (0x00,0xc2,0xac,0xac)},
            {'ł', (0xf8,0x00,0x00,0x9b)},
            {'m', (0x6d,0x6d,0x6d,0x6d)},
            {'¯', (0xc5,0xf8,0xaf,0xaf)},
            {'−', (0x00,0x00,0x00,0x8a)},
            {'μ', (0x00,0xb5,0xb5,0xb5)},
            {'×', (0x00,0x00,0xd7,0xd7)},
            {'n', (0x6e,0x6e,0x6e,0x6e)},
            {'9', (0x39,0x39,0x39,0x39)},
            {'ñ', (0x00,0x96,0xf1,0xf1)},
            {'#', (0x23,0x23,0x23,0x23)},
            {'o', (0x6f,0x6f,0x6f,0x6f)},
            {'ó', (0x00,0x97,0xf3,0xf3)},
            {'ô', (0x00,0x99,0xf4,0xf4)},
            {'ö', (0x00,0x9a,0xf6,0xf6)},
            {'œ', (0xfa,0xcf,0x9c,0x9c)},
            {'˛', (0xce,0xfe,0x00,0x1d)},
            {'ò', (0x00,0x98,0xf2,0xf2)},
            {'1', (0x31,0x31,0x31,0x31)},
            {'½', (0x00,0x00,0xbd,0xbd)},
            {'¼', (0x00,0x00,0xbc,0xbc)},
            {'¹', (0x00,0x00,0xb9,0xb9)},
            {'ª', (0xe3,0xbb,0xaa,0xaa)},
            {'º', (0xeb,0xbc,0xba,0xba)},
            {'ø', (0xf9,0xbf,0xf8,0xf8)},
            {'õ', (0x00,0x9b,0xf5,0xf5)},
            {'p', (0x70,0x70,0x70,0x70)},
            {'¶', (0xb6,0xa6,0xb6,0xb6)},
            {'(', (0x28,0x28,0x28,0x28)},
            {')', (0x29,0x29,0x29,0x29)},
            {'%', (0x25,0x25,0x25,0x25)},
            {'.', (0x2e,0x2e,0x2e,0x2e)},
            {'·', (0xb4,0xe1,0xb7,0xb7)},
            {'‰', (0xbd,0xe4,0x89,0x8b)},
            {'+', (0x2b,0x2b,0x2b,0x2b)},
            {'±', (0x00,0xb1,0xb1,0xb1)},
            {'q', (0x71,0x71,0x71,0x71)},
            {'?', (0x3f,0x3f,0x3f,0x3f)},
            {'¿', (0xbf,0xc0,0xbf,0xbf)},
            {'"', (0x22,0x22,0x22,0x22)},
            {'„', (0xb9,0xe3,0x84,0x8c)},
            {'“', (0xaa,0xd2,0x93,0x8d)},
            {'”', (0xba,0xd3,0x94,0x8e)},
            {'‘', (0x60,0xd4,0x91,0x8f)},
            {'’', (0x27,0xd5,0x92,0x90)},
            {'‚', (0xb8,0xe2,0x82,0x91)},
            {'\'', (0xa9,0x27,0x27,0x27)},
            {'r', (0x72,0x72,0x72,0x72)},
            {'®', (0x00,0xa8,0xae,0xae)},
            {'˚', (0xca,0xfb,0x00,0x1e)},
            {'s', (0x73,0x73,0x73,0x73)},
            {'š', (0x00,0x00,0x9a,0x9d)},
            {'§', (0xa7,0xa4,0xa7,0xa7)},
            {';', (0x3b,0x3b,0x3b,0x3b)},
            {'7', (0x37,0x37,0x37,0x37)},
            {'6', (0x36,0x36,0x36,0x36)},
            {'/', (0x2f,0x2f,0x2f,0x2f)},
            {' ', (0x20,0x20,0x20,0x20)},
            {'£', (0xa3,0xa3,0xa3,0xa3)},
            {'t', (0x74,0x74,0x74,0x74)},
            {'þ', (0x00,0x00,0xfe,0xfe)},
            {'3', (0x33,0x33,0x33,0x33)},
            {'¾', (0x00,0x00,0xbe,0xbe)},
            {'³', (0x00,0x00,0xb3,0xb3)},
            {'˜', (0xc4,0xf7,0x98,0x1f)},
            {'™', (0x00,0xaa,0x99,0x92)},
            {'2', (0x32,0x32,0x32,0x32)},
            {'²', (0x00,0x00,0xb2,0xb2)},
            {'u', (0x75,0x75,0x75,0x75)},
            {'ú', (0x00,0x9c,0xfa,0xfa)},
            {'û', (0x00,0x9e,0xfb,0xfb)},
            {'ü', (0x00,0x9f,0xfc,0xfc)},
            {'ù', (0x00,0x9d,0xf9,0xf9)},
            {'_', (0x5f,0x5f,0x5f,0x5f)},
            {'v', (0x76,0x76,0x76,0x76)},
            {'w', (0x77,0x77,0x77,0x77)},
            {'x', (0x78,0x78,0x78,0x78)},
            {'y', (0x79,0x79,0x79,0x79)},
            {'ý', (0x00,0x00,0xfd,0xfd)},
            {'ÿ', (0x00,0xd8,0xff,0xff)},
            {'¥', (0xa5,0xb4,0xa5,0xa5)},
            {'z', (0x7a,0x7a,0x7a,0x7a)},
            {'ž', (0x00,0x00,0x9e,0x9e)},
            {'0', (0x30,0x30,0x30,0x30)}
        };
        static InternalEncoding() 
        {
            STD = new Char[256];
            MAC = new Char[256];
            WIN = new Char[256];
            PDF = new Char[256];
            foreach (var kv in charSet) 
            {
                STD[kv.Value.std] = kv.Key;
                MAC[kv.Value.mac] = kv.Key;
                WIN[kv.Value.win] = kv.Key;
                PDF[kv.Value.pdf] = kv.Key;
            }
            STD[0] = default;
            MAC[0] = default;
            WIN[0] = default;
            PDF[0] = default;
        }

        public static String GetStdStr(ReadOnlySpan<Byte> data)
        {
            var sb = new StringBuilder(data.Length);
            for (var i = 0; i < data.Length; i++)
            {
                sb.Append(STD[data[i]]);
            }
            return sb.ToString();
        }

        public static String GetMacStr(ReadOnlySpan<Byte> data)
        {
            var sb = new StringBuilder(data.Length);
            for (var i = 0; i < data.Length; i++)
            {
                sb.Append(MAC[data[i]]);
            }
            return sb.ToString();
        }

        public static String GetWinStr(ReadOnlySpan<Byte> data)
        {
            var sb = new StringBuilder(data.Length);
            for (var i = 0; i < data.Length; i++)
            {
                sb.Append(WIN[data[i]]);
            }
            return sb.ToString();
        }

        public static String GetPDFStr(ReadOnlySpan<Byte> data)
        {
            var sb = new StringBuilder(data.Length);
            for (var i = 0; i < data.Length; i++)
            {
                sb.Append(PDF[data[i]]);
            }
            return sb.ToString();
        }
    }
}
